<?php
/**
 * created by zzy
 * date: 2018/1/26 14:07
 */


namespace App\Api\Service\Logistics\Controllers;

use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\LogisticsInvoice;
use Framework\BaseClass\Api\Controller;
use App\Engine\Func;

class InvoiceController extends Controller
{
    public function index(){}
    public function lists()
    {
        $userId             = Func::getHeaderValueByName('userid',25);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $notesId            = request('id',0);
        if( !$userInfo || !$notesId ){
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $userInfo->company_id;
        $where              = ['notes_id'=>$notesId];
        $collection         = LogisticsInvoice::getList($where);
        $lists              = $collection->toArray();
        foreach($lists as $key=>$val)
        {
            $imgImg         = Func::getImgUrlByIds($val['img_ids']);
            $lists[$key]['img_img'] = $imgImg;
        }
        return $lists;
    }
    public function edit()
    {
        $userId             = Func::getHeaderValueByName('userid',1);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        if( !$userInfo ){
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $userInfo->company_id;
        $isSubmit           = request('is_submit',0);
        //$notesId            = request('id',0);
        if($isSubmit){
            $data                   = request()->all();
            $data['company_id']   = $companyId;
            $fieldList              = LogisticsInvoice::$tableFieldList;
            $tableData              = LogisticsInvoice::setDataByTableField($data,$fieldList);
            $tableDataCheckResult   = LogisticsInvoice::checkDataByTableField($tableData,$fieldList);
            if($tableDataCheckResult !== true)
            {
                xThrow(ERR_PARAMETER, $tableDataCheckResult);
            }
            $editResult             = LogisticsInvoice::edit($tableData);
            if($editResult===false)
            {
                xThrow(ERR_UNKNOWN);
            }
            $result                 = ['message'=>'编辑成功'];
            return $result;
        } else {
            $result                 = [];
            return $result;
        }
    }
    public function del()
    {
        $userId             = Func::getHeaderValueByName('userid');
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $id                 = request('id',0);
        if( !$userInfo || !$id ){
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $userInfo->company_id;
        $where              = ['company_id'=>$companyId,'id'=>$id];
        $data               = ['status'=>2];
        $result             = LogisticsBill::updateOneData($where,$data);
        if($result){
            return ['删除成功'];
        }else{
            xThrow(ERR_UNKNOWN);
        }
    }
}